﻿<!--
 Copyright 2013 Shawn Henry, Rob Smith, and Michael Friedman

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-->
<view:AViewPage 
    x:Class="OneBusAway.WP7.View.DetailsPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:navigation="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:m="clr-namespace:Microsoft.Phone.Controls.Maps;assembly=Microsoft.Phone.Controls.Maps"
    xmlns:data="clr-namespace:OneBusAway.WP7.ViewModel;assembly=OneBusAway.WP7.ViewModel" 
    xmlns:view="clr-namespace:OneBusAway.WP7.View"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:shapes="clr-namespace:System.Windows.Shapes;assembly=System.Windows"
    xmlns:tilt="clr-namespace:ControlTiltEffect"
    xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
    tilt:TiltEffect.IsTiltEnabled="True"
    SupportedOrientations="Portrait"
    mc:Ignorable="d" d:DesignHeight="696" d:DesignWidth="480"
    shell:SystemTray.IsVisible="True"
    xmlns:localControls="clr-namespace:Microsoft.Phone.Controls"
    >

    <view:AViewPage.Resources>

        <data:RouteDetailsVM x:Key="ViewModel" />

        <view:DateTimeDeltaConverter x:Key="DateTimeDeltaConverter" />
        <view:DateTimeConverter x:Key="DateTimeConverter" />
        <view:VisibilityConverter x:Key="VisibilityConverter" />
        <view:DelayColorConverter x:Key="DelayColorConverter" />
        <view:PolylineConverter x:Key="PolylineConverter" />
        <view:PushpinScaleConverter x:Key="PushpinScaleConverter" />
        <view:LateEarlyConverter x:Key="LateEarlyConverter" />
        <view:LowercaseConverter x:Key="LowercaseConverter" />
        
        <!-- TextBox styles -->

        <DataTemplate x:Key="RouteTemplate">
            <Grid Margin="10,5,10,10">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="80" />
                    <ColumnDefinition Width="140" />
                    <ColumnDefinition Width="135" />
                    <ColumnDefinition Width="105" />
                </Grid.ColumnDefinitions>

                <Grid.RowDefinitions>
                    <RowDefinition Height="26" />
                    <RowDefinition Height="24" />
                </Grid.RowDefinitions>

                <toolkit:ContextMenuService.ContextMenu>
                    <toolkit:ContextMenu IsZoomEnabled="False">
                        <toolkit:MenuItem Header="zoom to here" Click="ZoomToBus_Click" />
                        <toolkit:MenuItem Header="notify me" Click="NotifyArrival_Click" />
                    </toolkit:ContextMenu>
                </toolkit:ContextMenuService.ContextMenu>
                <TextBlock 
                    Grid.Column="0" 
                    Grid.Row="0" 
                    Grid.RowSpan="2" 
                    Text="{Binding routeShortName}" 
                    FontSize="{StaticResource PhoneFontSizeLarge}" 
                    Foreground="{StaticResource OBAForegroundBrush}"
                    VerticalAlignment="Top"
                    Margin="0,-3,5,0"
                    />
                <TextBlock 
                    Grid.Column="1" 
                    Grid.ColumnSpan="2" 
                    Grid.Row="0" 
                    FontSize="{StaticResource PhoneFontSizeNormal}" 
                    Foreground="{StaticResource OBAForegroundBrush}"       
                    TextAlignment="Left"
                    Text="{Binding tripHeadsign}" 
                    />
                <TextBlock 
                    Grid.Column="1" 
                    Grid.Row="1" 
                    Foreground="{StaticResource OBASubtleBrush}" 
                    FontSize="{StaticResource PhoneFontSizeSmall}"
                    Text="{Binding scheduledDepartureTime, Converter={StaticResource DateTimeConverter}, ConverterParameter=Sched:}" 
                    />
                <TextBlock 
                    Grid.Column="2" 
                    Grid.Row="1" 
                    FontSize="{StaticResource PhoneFontSizeSmall}"
                    TextAlignment="Right"
                    HorizontalAlignment="Stretch"
                    Text="{Binding busDelay, Converter={StaticResource LateEarlyConverter}}" 
                    Foreground="{Binding Converter={StaticResource DelayColorConverter}}"
                    />
                <TextBlock 
                    Grid.Column="3" 
                    Grid.Row="0" 
                    Grid.RowSpan="2" 
                    Style="{StaticResource PhoneTextLargeStyle}" 
                    TextAlignment="Right" 
                    VerticalAlignment="Top" 
                    Margin="0,-3,0,0"
                    Text="{Binding Path=nextKnownDeparture, Converter={StaticResource DateTimeDeltaConverter}}" 
                    Foreground="{Binding Converter={StaticResource DelayColorConverter}}"
                    />

            </Grid>
        </DataTemplate>

        <Style x:Key="BusStopLargePushpinStyle" TargetType="m:Pushpin">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate>
                        <Button 
                            Width="50" Height="50"
                            HorizontalContentAlignment="Stretch" 
                            VerticalContentAlignment="Stretch" 
                            BorderThickness="0" 
                            Padding="0" 
                            Click="BusStopPushpin_Click"
                            Tag="{TemplateBinding Tag}"
                            >
                            <Image Source="/Bus_Icon.png" Margin="-3"/>
                        </Button>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </view:AViewPage.Resources>

    <Grid x:Name="LayoutRoot" Background="{StaticResource OBAPrimaryBrush}">
        <Grid.RowDefinitions>
            <RowDefinition Height="70"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="200"/>
        </Grid.RowDefinitions>

        <Grid Grid.Row="0" x:Name="TitleGrid"
              d:DataContext="{d:DesignData Source=SampleData/DetailsViewModelSampleData.xaml}">
            <StackPanel Orientation="Horizontal">
                <TextBlock 
                    VerticalAlignment="Top"
                    x:Name="RouteNumber" 
                    Foreground="{StaticResource OBAForegroundBrush}" 
                    Text="{Binding Path=CurrentViewState.CurrentRoute.shortName}" 
                    FontSize="48" 
                    Margin="10,0,10,0" 
                    />

                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="1*" />
                        <RowDefinition Height="1*" />
                    </Grid.RowDefinitions>

                    <TextBlock Grid.Row="0" x:Name="RouteName" Foreground="{StaticResource OBAForegroundBrush}" Text="{Binding Path=CurrentViewState.CurrentRouteDirection.name}" FontSize="{StaticResource PhoneFontSizeMedium}"/>
                    <TextBlock Grid.Row="1" x:Name="RouteInfo" Foreground="{StaticResource OBAForegroundBrush}" Text="{Binding Path=CurrentViewState.CurrentStop.name}" FontSize="{StaticResource PhoneFontSizeMedium}"/>
                </Grid>
            </StackPanel>

        </Grid>

        <Grid Grid.Row="1" x:Name="ContentGrid">
            <m:Map x:Name="DetailsMap" CopyrightVisibility="Collapsed" LogoVisibility="Collapsed" ScaleVisibility="Collapsed" CredentialsProvider="{StaticResource MapCredentials}"
                   Center="{Binding Path=LocationTracker.CurrentLocationSafe, Source={StaticResource ViewModel}}"  ZoomLevel="10"
                    MapZoom="DetailsMap_MapZoom">
                <m:MapLayer x:Name="RouteLineLayer">
                    <m:MapItemsControl x:Name="RouteLineControl" ItemsSource="{Binding Path=CurrentViewState.CurrentRouteDirection, Converter={StaticResource PolylineConverter}}" DataContext="{StaticResource ViewModel}">
                        <m:MapItemsControl.ItemTemplate>
                            <DataTemplate>
                                <m:MapPolyline 
                                Locations="{Binding}" 
                                StrokeThickness="5"
                                Stroke="{StaticResource OBAAccentBrush}"
                                />
                            </DataTemplate>
                        </m:MapItemsControl.ItemTemplate>
                    </m:MapItemsControl>
                </m:MapLayer>
                <m:MapLayer x:Name="BusTripsLayer">
                    <m:MapItemsControl x:Name="BusTripsItemsControl" ItemsSource="{Binding Path=ArrivalsForStop, Source={StaticResource ViewModel}}">
                        <m:MapItemsControl.ItemTemplate>
                            <DataTemplate>
                                <m:Pushpin 
                                    Location="{Binding tripDetails.location}" 
                                    PositionOrigin="Center" 
                                    Style="{StaticResource BusTripPushpinStyle}" 
                                    Visibility="{Binding tripDetails.locationKnown, Converter={StaticResource VisibilityConverter}}"
                                    />
                            </DataTemplate>
                        </m:MapItemsControl.ItemTemplate>
                    </m:MapItemsControl>
                </m:MapLayer>
                <m:MapLayer x:Name="BusStopsLayer" Visibility="Collapsed">
                    <m:MapItemsControl x:Name="BusStopItemsControl" ItemsSource="{Binding Path=CurrentViewState.CurrentRouteDirection.stops}" DataContext="{StaticResource ViewModel}">
                        <m:MapItemsControl.ItemTemplate>
                            <DataTemplate>
                                <m:Pushpin 
                                    Location="{Binding location}" 
                                    PositionOrigin="Center" 
                                    Tag="{Binding Path=id}" 
                                    Style="{StaticResource BusStopLargePushpinStyle}" 
                                    />
                                <!-- 
                                    TODO: Enable this once I get it working
                                    RenderTransform="{Binding Path=ZoomLevel, ElementName=DetailsMap, Converter={StaticResource PushpinScaleConverter}}"
                                -->
                            </DataTemplate>
                        </m:MapItemsControl.ItemTemplate>
                    </m:MapItemsControl>
                </m:MapLayer>
                <m:MapLayer x:Name="SelectedStopLayer">
                    <m:Pushpin x:Name="StopPushpin" Location="{Binding Path=CurrentViewState.CurrentStop.location}" DataContext="{StaticResource ViewModel}" PositionOrigin="BottomLeft">
                        <Image Source="/Bus_Icon.png" Width="25" Height="25" />
                    </m:Pushpin>
                </m:MapLayer>
                <m:MapLayer x:Name="MyLocationLayer">
                    <m:Pushpin Location="{Binding Path=LocationTracker.CurrentLocationSafe, Source={StaticResource ViewModel}}" PositionOrigin="Center" Style="{StaticResource MyLocationPushpinStyle}" />
                </m:MapLayer>
            </m:Map>
        </Grid>
        <Grid Grid.Row="2" x:Name="StopGrid">

            <ListBox 
                x:Name="ArrivalsListBox" 
                d:DataContext="{d:DesignData Source=SampleData/DetailsViewModelSampleData.xaml}"
                ItemsSource="{Binding Path=ArrivalsForStop}" 
                ItemTemplate="{StaticResource RouteTemplate}" 
                SelectionChanged="ArrivalsListBox_SelectionChanged"
                LayoutUpdated="ArrivalsListBox_LayoutUpdated"
                 
                />
            <TextBlock x:Name="NoResultsTextBlock" Text="no results" 
                       Visibility="{Binding Path=NoResultsAvailable, Source={StaticResource ViewModel}, Converter={StaticResource VisibilityConverter}}"
                       FontSize="{StaticResource PhoneFontSizeLarge}" 
                       Foreground="{StaticResource OBAForegroundBrush}"
                       VerticalAlignment="Top"
                       Margin="12" 
                       />
        </Grid>

        <localControls:PerformanceProgressBar
            VerticalAlignment="Top"
            IsLoading="{Binding Path=operationTracker.Loading, Source={StaticResource ViewModel}}"
            Foreground="{StaticResource OBALightBrush}"
            Style="{StaticResource PerformanceProgressBarStyle}"
            />

    </Grid>

    <navigation:PhoneApplicationPage.ApplicationBar >
        <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True" >
            <shell:ApplicationBarIconButton x:Name="appbar_favorite" IconUri="/Images/appbar.favs.addto.rest.png" Text="Favorite" Click="appbar_favorite_Click" />
            <shell:ApplicationBarIconButton x:Name="appbar_refresh" IconUri="/Images/appbar.refresh.rest.png" Text="Refresh" Click="appbar_refresh_Click" />
        </shell:ApplicationBar>
    </navigation:PhoneApplicationPage.ApplicationBar>
</view:AViewPage>
